﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Windows.Forms;

namespace SantaSLittleIrcHelper
{
    public class Languages
    {

        public Dictionary<String, String> languages = new Dictionary<string, string>();
        public Dictionary<String, Dictionary<String, String>> objects = new Dictionary<string, Dictionary<String, String>>();

        private static Languages instance = null;
        private static readonly object padlock = new object();

        public static Languages Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new Languages();
                    }
                    return instance;
                }
            }
        }

        private Languages()
        {


        }

        public void save()
        {

            XDocument main = new XDocument();
            XElement tree = new XElement("localization");
            XElement languagesTree = new XElement("languages");
            XElement objectTree = new XElement("objects");

            foreach (KeyValuePair<String, String> language in this.languages)
            {
                XElement temp = new XElement("lang", language.Value);
                temp.Add(new XAttribute("code", language.Key));
                languagesTree.Add(temp);
            }
            tree.Add(languagesTree);

            foreach (KeyValuePair<String, Dictionary<String, String>> langObject in this.objects)
            {
                XElement langObjectElement = new XElement("object");
                langObjectElement.Add(new XAttribute("name", langObject.Key));
                foreach (KeyValuePair<String, String> objectThisInner in langObject.Value)
                {
                    XElement temp = new XElement("element", objectThisInner.Value);
                    temp.Add(new XAttribute("lang", objectThisInner.Key));
                    langObjectElement.Add(temp);
                }
                objectTree.Add(langObjectElement);
            }
            tree.Add(objectTree);
            main.Add(tree);
            main.Save(@"localization.xml");
        }


        public String get(String what)
        {
            return this.get(what,new String[0]);
        }

public String get(String what, params String[] args)
{
    Dictionary<String, String> translations;
    String localizedString;
    if (objects.TryGetValue(what, out translations))
    {
        if (translations.TryGetValue(Program.instance.settings.currentLanguage, out localizedString))
        {
            // String wurde gefunden
            if (args != null && args.Length > 0)
            {
                return String.Format(localizedString, args);
            }
            else
            {
                return localizedString;
            }
        }
        else if (translations.TryGetValue(Program.instance.settings.defaultLanguage, out localizedString))
        {
            // String wurde nicht gefunden, es wird stattdessen der String für die Standard-Sprache
            // mit einem Vermerk gespeichert und zurückgegeben
            translations[Program.instance.settings.currentLanguage] = ("!NYT! " + localizedString);
            return "!NYT! " + localizedString;
        }
        else
        {
            // Standard-Sprache wurde ebenso nicht gefunden. Programm läuft nun alle verfügbaren Sprachen
            // durch und erstellt Vermerkeinträge um die Übersetzung zu erleichtern.
            foreach (KeyValuePair<String, String> language in languages)
            {
                translations[language.Key] = ("!NYI! " + what);
            }
            translations[Program.instance.settings.currentLanguage] = ("!NYI! " + what);
            return ("!NYI! " + what);
        }
    }
    else
    {
        // Das gesuchte Objekt existiert in den Daten nicht. Es wird ein leer-Eintrag erstellt.
        objects[what] = new Dictionary<string, string>();
        return this.get(what);
    }
}

        public void translateAll()
        {
            changeLanguage(Program.instance.settings.currentLanguage);
        }

        public void changeLanguage(String newLanguage)
        {
            String[] textEntities = { "buttonRegister", "buttonAvailability", "buttonConnect", "buttonDisconnect", "labelStatus", "labelUsername", "labelPassword", "labelEmail", "labelConfirmation", "buttonConfirm", "labelInformation", "labelRegistrationFeedback", "labelCheckAvailabilityFeedback", "assistantErrorLabel", "assistantStateLabel", "checkBoxAutoUnban", "tabPage1", "tabPage2", "tabPage3", "tabPage4" };
            String[] menuStripEntities = { "debugModeToolStripMenuItem", "settingsToolStripMenuItem", "verboseModeToolStripMenuItem" };
            Program.instance.settings.currentLanguage = newLanguage;


            foreach (String entity in textEntities)
            {
                Control x = Program.instance.GUI.Controls.Find(entity, true)[0];

                if (x is TLabel)
                {
                    ((TLabel)x).translateThis();
                } else {
                    x.Text = this.get(entity);
                }
                //Program.instance.GUI.Controls.Find(entity, true)[0].Text = this.get(entity);
            }
            MenuStrip strip = (MenuStrip)Program.instance.GUI.Controls.Find("menuStrip1", true)[0];
            foreach (String entity in menuStripEntities)
            {
                strip.Items.Find(entity, true)[0].Text = this.get(entity);
            }
            Program.instance.connectionStatus.update();
        }

        public void addLanguage(String code, String verbose)
        {
            this.languages.Add(code, verbose);
        }


        public void load()
        {

            XElement tree = XElement.Load(@"localization.xml");
            XElement languagesTree = tree.Element("languages");

            foreach (XElement language in languagesTree.Elements("lang"))
            {
                this.languages.Add(language.Attribute("code").Value, language.Value);
            }

            XElement objectsTree = tree.Element("objects");

            foreach (XElement objectThis in objectsTree.Elements("object"))
            {
                Dictionary<String, String> objectThisDictionary = new Dictionary<string, string>();
                this.objects.Add(objectThis.Attribute("name").Value, objectThisDictionary);

                foreach (XElement objectThisInner in objectThis.Elements("element"))
                {
                    objectThisDictionary.Add(objectThisInner.Attribute("lang").Value, objectThisInner.Value);
                }
            }
        }


    }
}